package com.example.administrator.boltshow;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.DetaisRecycleAdapter;
import config.NetConfig;
import model.BoltDetails;

public class BoltDetailsActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private TextView mTextView;
    private ImageView mImageView;
    private DetaisRecycleAdapter mAdapter;
    private List<BoltDetails.DataBean> mData;
    private GridLayoutManager mGridLayoutManager;
    private String mLinkid;
    private int mCurrentPosition = 1;
    private ImageOptions mImageOptions;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bolt_details);
        initView();
        initData();
        setData();
        setListener();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_bolt_details_show);
        mTextView = (TextView) findViewById(R.id.tv_bolt_details_title);
        mImageView = (ImageView) findViewById(R.id.iv_bolt_details_title);
    }

    private void initData() {
        mImageOptions = new ImageOptions.Builder().setUseMemCache(true).build();
        mData = new ArrayList<>();
        mAdapter = new DetaisRecycleAdapter(mData, this);
        mGridLayoutManager = new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false);
        //获取传过来的数据
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        mLinkid = bundle.getString("linkid");
        loadMore(mCurrentPosition);

    }

    private void setData() {
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void setListener() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int position = mGridLayoutManager.findLastVisibleItemPosition();
                    if (position == mData.size() - 1) {
                        loadMore(mCurrentPosition);
                    }
                }
            }
        });
        //传值到shopactivity linkid,goodsId
        mAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer position = (Integer) v.getTag();
                if(position!=null) {
                    BoltDetails.DataBean dataBean = mData.get(position);
                    String goodsId = dataBean.getGoodsId();
                    Bundle bundle = new Bundle();
                    bundle.putString("linkid",mLinkid);
                    bundle.putString("goodsid",goodsId);
                    Intent intent = new Intent(BoltDetailsActivity.this,ShopActivity.class);
                    intent.putExtra("bundle",bundle);
                    startActivity(intent);
                }
            }
        });
    }

    private void loadMore(final int position) {
        String path = String.format(NetConfig.BOLT_DETAILS, Integer.parseInt(mLinkid), position);
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    Gson gson = new Gson();
                  /*  if(position==1) {
                        mData.clear();
                    }*/
                    mCurrentPosition++;
                    BoltDetails boltDetails = gson.fromJson(result, BoltDetails.class);
                    mTextView.setText(boltDetails.getTitle());
                    x.image().bind(mImageView, boltDetails.getTopImg(), mImageOptions);
                    mData.addAll(boltDetails.getData());
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
